package com.smit.player;

import android.media.MediaPlayer;

import com.smit.utils.LogUtil;

import java.io.IOException;

/**
 * SmitPlayer
 */
public final class SmitPlayer extends MediaPlayer {
    private static final String TAG = "SmitPlayer";
    private static final String SMIT_X_PLAYER_CLASS_NAME = "com.smit.player.SmitXPlayer";
    private static MediaPlayer player;
    private static final String DVB_TS_URL = "http://127.0.0.1:51818/10.ts";
    private static String playerInfo = "{\n"
            + "\"版本\": \"1.0\",\n"
            + "\"厂商\": \"SMiT\",\n"
            + "\"芯片方案\": \"NULL\",\n"
            + "\"设备型号\": \"NULL\",\n"
            + "\"维护人员\": \"NULL\",\n"
            + "\"支持视频\": \"NULL\",\n"
            + "\"支持音频\": \"NULL\",\n"
            + "\"发布日期\": \"NULL\"\n"
            + "}\n";

    private SmitPlayer() {

    }

    /**
     * 获取MediaPlayer
     *
     * @return MediaPlayer对象
     */
    public static MediaPlayer getPlayer() {
        if (player == null) {
            try {
                Class<?> classType = Class.forName(SMIT_X_PLAYER_CLASS_NAME); //检查系统是否有SmitXPlayer
                if (classType != null) {
                    player = new SmitXPlayer();
                    playerInfo = ((SmitXPlayer) player).getPlayerInfo();
                    LogUtil.info(TAG, SMIT_X_PLAYER_CLASS_NAME + " found");
                }
            } catch (ClassNotFoundException e) {
                player = new SmitPlayer();
                LogUtil.error(TAG, SMIT_X_PLAYER_CLASS_NAME + " NOT found, use SmitPlayer instead");
            }
        }
        return player;
    }

    /**
     * 获取player的一些信息
     * @return playerInfo String
     */
    public String getPlayerInfo() {
        return playerInfo;
    }

    /**
     * 重新设置TS流播放url
     * @throws IOException IO访问异常
     * @throws IllegalArgumentException 参数错误异常
     * @throws SecurityException 安全访问异常
     * @throws IllegalStateException 在一个非法的状态调用导致异常
     */
    public static void updateDataSource()
            throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        if (player != null) {
            player.setDataSource(DVB_TS_URL);
        }
    }
}